package com.dongdongshop.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.TbSellerServiceI;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class Realm extends AuthorizingRealm {

    @Reference
    private TbSellerServiceI tbSellerServiceI;

    /*执行授权逻辑
    * */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       /* System.out.println("执行授权逻辑");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //查询当前登录用户的perms权限
        TbSeller seller  = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        List<String> perms = menuServiceI.getPermsByPid(person.getPid());
        authorizationInfo.addStringPermissions(perms);
        //*/
        return null;
    }
    /*执行认证逻辑
    * */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");
        UsernamePasswordToken totken = (UsernamePasswordToken)authenticationToken;
        TbSeller tbSeller = tbSellerServiceI.listGet(totken.getUsername());
        if(tbSeller == null){
            return null;
        }
        if(!tbSeller.getStatus().equals("1")){
            return null;
        }
            return  new SimpleAuthenticationInfo(tbSeller,tbSeller.getPassword(),ByteSource.Util.bytes(tbSeller.getSalt()),tbSeller.getName());


}
}
